﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using XDL.Framework.Particles;
using XDL.Framework;
using JumpNFight.Simulation;

namespace JumpNFight.Weapons
{
    class RPGWeaponSystem : BulletWeaponSystemBase
    {
        ParticleSystem m_trailPS;
        ParticleSystem m_blastPS;

        public RPGWeaponSystem(Game game) :
            base(game)
        { }

        protected override string BulletPSName
        {
            get { return "Particles/RPGBullet"; }
        }

        protected override string WeaponName
        {
            get { return "Weapons/RPG"; }
        }

        public override void Initialize()
        {
            m_trailPS = GameServices.GetService<ParticleSystem>("Particles/SmallSmoke");
            m_blastPS = GameServices.GetService<ParticleSystem>("Particles/LargeBlast");
            base.Initialize();
        }

        /*
        protected override XDL.Framework.Particles.TrailEmitter GetTrailEmitter()
        {
            ParticleSystem ps = GameServices.GetService<ParticleSystem>("Particles/SmallSmoke");
            return new TrailEmitter(ps, 10);
        }
        */
        protected override int TrailCount
        {
            get
            {
                return 3;
            }
        }

        protected override bool ShakeWorldOnHit
        {
            get
            {
                return true;
            }
        }

        protected override ParticleSystem TrailParticleSystem
        {
            get
            {
                return m_trailPS;
            }
        }

        static Vector2 RandomUnitVector()
        {
            Vector2 v = Vector2.Zero;

            do
            {
                v = new Vector2(
                        (float)(Rnd.NextDouble() * 2) - 1,
                        (float)(Rnd.NextDouble() * 2) - 1);
            }
            while (v.LengthSquared() >= 1);

            return v;
        }

        protected override bool Hit(Simulation.Intersection intersection, int tag, ref ParticleVertex particle, Vector3 position, Vector3 normalizedDirection, float distance)
        {
            // spawn blast
            m_blastPS.AddParticle(position, Vector3.Zero);

            this.World.Shake();

            // this weapon has a blast sound
            Audio.PlayCue(Settings.Name + "/Blast");

            Vector2 pos = new Vector2(position.X,position.Y);
            // kill badguys
            foreach (BadGuy it in World.NPCs)
            {
                if (it.IsAlive )
                {
                    Vector2 velocityVector = it.Moveable.Position - pos;
                    float dist = velocityVector.LengthSquared();
                    if ( dist < 150 * 150 )
                    {
                        Vector2 velocity = Vector2.Zero;
                        if (dist < 1)
                            velocity = 600 * RandomUnitVector();
                        else
                            velocity = 500 * Vector2.Normalize(velocityVector);

                        GameServices.EventDispatcher.PostEvent(it.ID, NPCBase.HitEvent, new WeaponHitEventArgs(tag, this.Settings) { Velocity = velocity });
                        GameServices.EventDispatcher.PostEvent(tag, Player.RegisterHitEvent, null);
                    }
                }
            }
            // kill the particle
            return false;
        }
    }
}
